package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sgb.kjwl.R;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.user.PublicAttrEntity;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;

public class MachinePublishAttributeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /***
     * 弹窗选择后返回的值
     */
    private String val = "";
    /***
     * 上下文
     */
    private Context context;
    /***
     * 属性值 list
     */
    private List<FilterEntity<List<FilterEntity>>> datas;
    /***
     * 需要返回到上一个界面的实体
     */
    private PublicAttrEntity attributesEntity;
    /**
     * 返回到上一个界面的数据 list
     */
    private List<PublicAttrEntity> attributesEntityList;
    /**
     * 有数据时候，已经选择的数据
     */
    private List<PublicAttrEntity> alreadyChoice;
    /***
     * 确认按钮 type
     */
    private int TYPE_FOOT = 1001;
    /**
     * 是否选择数据
     */
    private boolean isNUll = false;
    /**
     * 是否第一次初始化adapter
     */
    private boolean iFirst;

    public MachinePublishAttributeAdapter(Context context, List<FilterEntity<List<FilterEntity>>> datas, List<PublicAttrEntity> alreadyChooce) {
        this.datas = datas;
        this.context = context;
        attributesEntityList = new ArrayList<>();
        this.alreadyChoice = alreadyChooce;
        init(true, "", 0);
    }

    /***
     * 设置选择的值并刷新列表
     * @param val
     * @param item
     */
    public void setVal(String val, int item) {
        this.val = val;
        init(false, val, item);
        notifyItemChanged(item);
    }

    /***
     * 获取 非必要属性的值
     * @param val
     * @param item
     */
    public void getMyVal(String val, int item) {
        this.val = val;
        init(false, val, item);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_FOOT) {
            View headerView = View.inflate(context, R.layout.item_ok_foot, null);
            return new FootViewHolder(headerView);
        } else {
            View v = View.inflate(context, R.layout.item_machine_publish_attribute, null);
            return new MyViewHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            if (datas.get(position).getAttrKey() == 0) {
                ((MyViewHolder) holder).contentTv.setVisibility(View.GONE);
                ((MyViewHolder) holder).contentEdt.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).imageView.setVisibility(View.INVISIBLE);
            } else {
                ((MyViewHolder) holder).contentTv.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).contentEdt.setVisibility(View.GONE);
            }
            ((MyViewHolder) holder).labelNameTv.setText(datas.get(position).getName());
            if (iFirst) {
                if (alreadyChoice != null) {
                    for (int i = 0; i < alreadyChoice.size(); i++) {
                        if (alreadyChoice.get(i).getAttributeNo().equals(datas.get(position).getNo())) {
                            attributesEntityList.add(alreadyChoice.get(i));
                            if (datas.get(position).getAttrKey() == 1) {
                                ((MyViewHolder) holder).contentTv.setText(alreadyChoice.get(i).getAttributeVal());
                            } else {
                                ((MyViewHolder) holder).contentEdt.setText(alreadyChoice.get(i).getAttributeVal());
                            }
                        }
                    }
                }
            } else if (!"".equals(val)) {
                ((MyViewHolder) holder).contentTv.setText(val);
            }
            if (mItemClickListener != null) {
                ((MyViewHolder) holder).rl.setOnClickListener(v -> mItemClickListener.onItemClick(position, ((MyViewHolder) holder).contentTv.getText().toString()));
            }
            ((MyViewHolder) holder).contentEdt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    MLog.d("-----", charSequence.toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    getMyVal(editable.toString(), position);
                }
            });
        } else if (holder instanceof FootViewHolder) {
            if (datas.size() == 0) {
                ((FootViewHolder) holder).ok_view.setVisibility(View.GONE);
                ((FootViewHolder) holder).no_data.setVisibility(View.VISIBLE);
            }
            ((FootViewHolder) holder).ok_view.setOnClickListener(view -> {
                if (attributesEntityList == null || attributesEntityList.size() <= 0) {
                    Toast.makeText(context, "请选择或填写属性值", Toast.LENGTH_SHORT).show();
                    return;
                }
                for (int i = 0; i < attributesEntityList.size(); i++) {
                    if (attributesEntityList.get(i) != null) {
                        if ("1".equals(attributesEntityList.get(i).getKey()) && TextUtils.isEmpty(attributesEntityList.get(i).getAttributeVal())) {
                            Toast.makeText(context, "请选择或填写属性值", Toast.LENGTH_SHORT).show();
                            isNUll = true;
                            return;
                        }
                    }

                }
                if (filterResultClick != null) {
                    filterResultClick.result(attributesEntityList);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        //在第一个位置添加头
        if (position == datas.size()) {
            return TYPE_FOOT;
        }
        return super.getItemViewType(position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView contentTv;
        TextView labelNameTv;
        RelativeLayout rl;
        EditText contentEdt;
        ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            contentTv = itemView.findViewById(R.id.content_tv);
            labelNameTv = itemView.findViewById(R.id.label_name_tv);
            rl = itemView.findViewById(R.id.rl);
            contentEdt = itemView.findViewById(R.id.content_edt);
            imageView = itemView.findViewById(R.id.arrow_right_img);
        }
    }

    /**
     * 尾布局的viewholder
     */
    class FootViewHolder extends RecyclerView.ViewHolder {
        Button ok_view;
        RelativeLayout no_data;

        public FootViewHolder(View itemView) {
            super(itemView);
            ok_view = itemView.findViewById(R.id.ok_view);
            no_data = itemView.findViewById(R.id.no_data);
        }
    }

    //点击事件
    private ItemClickListener mItemClickListener;

    public interface ItemClickListener {
        void onItemClick(int position, String val);
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

    /***
     * 初始化数据
     * @param iFirst 是否第一次初始化
     * @param val 选择的数据值
     * @param item 点击的位置
     */
    public void init(boolean iFirst, String val, int item) {
        this.iFirst = iFirst;
        /*if (iFirst && attributesEntityList == null) {

        } else*/
        if (!iFirst && attributesEntityList != null) {
            if (attributesEntityList.size() == 0) {
                for (int i = 0; i < datas.size(); i++) {
                    attributesEntity = new PublicAttrEntity();
                    attributesEntity.setKey(datas.get(i).getAttrKey() + "");
                    attributesEntity.setAttributeNo(datas.get(i).getNo());
                    attributesEntity.setName(datas.get(i).getName());
                    attributesEntityList.add(attributesEntity);
                }
            }
            if (attributesEntityList.size() != 0) {
                attributesEntityList.get(item).setAttributeVal(val);
            }
        }
    }

    private FilterResultClick filterResultClick;

    public interface FilterResultClick {
        void result(List<PublicAttrEntity> attributesEntityList);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }
}